<?php

namespace app\modules\v1\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use app\modules\v1\models\Log;

class PayController extends ActiveController
{
    public $modelClass = '';
    
    public function actions(){
        
        $actions = parent::actions();
        
        unset($actions['index'],$actions['create'],$actions['delete'],$actions['view'],$actions['update']);
        
    }
    
    public function actionIndex(){
        return "Pay Api";
    }
    
    public function actionCreate(){
        $Raw = Yii::$app->request->getRawBody();
        $log = new Log();
        $log->attributes = ['operation' => 'Post','class' => 'Pay','requestData' => $Raw,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
        if(!$log->save()){
            return $log->getFirstError();
        }
        $array = json_decode($Raw,true);
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $out_trade_no = time();     /*十位数*/
        $nonce_str = md5($out_trade_no);
        $spill_create_ip = Yii::$app->request->userIP;
        $money = $array["money"];
        /**
         * 第一次签名
         */
        $sign_1 = strtoupper(md5(
                "appid=wx96867f9488a069b8&attach=微信支付平台"
                . "&body=掌上大律师充值业务"
                . "&mch_id=1465599702"
                . "&nonce_str=".$nonce_str
                . "&notify_url=http://42.51.32.179/api/web/v1/notifies"
                . "&out_trade_no=".$out_trade_no
                . "&spbill_create_ip=".$spill_create_ip
                . "&total_fee=".$money
                . "&trade_type=APP"
                . "&key=b81b007aca698c20aedb3a611d4f9081"));
        $post_data = "<xml>
                        <appid>wx96867f9488a069b8</appid>
                        <attach>微信支付平台</attach>
                        <body>掌上大律师充值业务</body>
                        <mch_id>1465599702</mch_id>
                        <nonce_str>". $nonce_str."</nonce_str>
                        <notify_url>http://42.51.32.179/api/web/v1/notifies</notify_url>
                        <out_trade_no>".$out_trade_no."</out_trade_no>
                        <spbill_create_ip>".$spill_create_ip."</spbill_create_ip>
                        <total_fee>".$money."</total_fee>
                        <trade_type>APP</trade_type>
                        <sign>".$sign_1."</sign>
                        </xml>";

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        $output = curl_exec($ch);
        curl_close($ch);

        //WX返回数据XML转数组
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($output,'SimpleXMLElement',LIBXML_NOCDATA);
        $xmlToArray = json_decode(json_encode($xmlstring),true);
        
        /**
         * 第二次签名
         */
        $sign_2 = strtoupper(md5(
                "appid="
                . "&partnerid=1465599702"
                . "&prepayid=".$xmlToArray["prepay_id"]
                . "&noncestr=".$xmlToArray["nonce_str"]
                . "&timestamp=".$out_trade_no
                . "&package=Sign=WXPay"));
        
        $output2 = [
            "appId" => "wx96867f9488a069b8",
            "partnerId" => "1465599702",
            "prepayId" => $xmlToArray["prepay_id"],
            "nonceStr" => $xmlToArray["nonce_str"],
            "timeStamp" => $out_trade_no,
            "package" => "Sign=WXPay",
            "sign" => $sign_2
            ];
        
        Yii::$app->response->format=\yii\web\Response::FORMAT_JSON;
        return $output2;
    }
    
    public function actionUpdate(){
        return "Pay Api";
    }
    
    public function actionDelete(){
        return "Pay Api";
    }
    
    public function actionView(){
        return "Pay Api";
    }
    
    
//    protected function log($op,$data){
//        
//        $log = new Log();
//        
//        $log->attributes = ['operation' => $op,'class' => 'Lawyer','requestData' => $data,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
//        
//        if(!$log->save()){
//            return $log->getFirstError();
//        }
//        
//    }
    
//    public function tokenValidate($token){
//        
//        if($token != Yii::$app->params['token']){
//            throw new NotFoundHttpException;
//        }else{
//            return true;
//        }
//        
//    }

}
